const enterpriseVue = new Vue({
  el: '#app',
  data() {
    //校验身份证号
    var checkIDCard = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('身份证号不能为空'));
      }
      setTimeout(() => {
        if (!ID_Validity(value)) {
          callback(new Error('请输入正确的身份证号'));
        }
      }, 100);
    }
    //校验手机号
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'));
      }
      setTimeout(() => {
        if (!validatemobile(value)) {
          callback(new Error('请输入正确的手机号'));
        } else {
          callback();
        }
      }, 100);
    }
    return {
      pickerOptions: pickerOptions,
      rules: {
        Employee_Name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        Mobile: [{ validator: checkIDCard, trigger: 'blur' }],
        ID_Number: [{ validator: checkIDCard, trigger: 'blur' }]
      },
      form: {
        Employee_SerialNumber: "",
        Employee_Name: '',
        Mobile: '',
        ID_Number: '',
        Gender: '',
        Nation: '',
        Education: '',
        Residence_Address: '',
        Current_Residence_Address: '',
        Household_Register_Class: '',
        Entry_Date: '',
        Departure_Date: '',
        Contract_SerialNumber: '',
        Supplementary_Insurance: 0,
      },
      loading: false
    }
  },
  mounted() {
    const Employee_SerialNumber = sessionStorage.getItem("Employee_SerialNumber")
    this.getData(Employee_SerialNumber)
  },
  methods: {
    getData(Employee_SerialNumber) {
      const _this = this
      $.ajax({
        url: DomainName + '/Employee_Edit',
        type: 'POST',
        data: {
          Data: Employee_SerialNumber,
          token: $.cookie("token")
        },
        success: function (res) {
          console.log('根据企业编号获取结果', res)
          _this.form = res.employee[0]
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { add, modify } = _this.deal()
          let data = {}
          if (add == null) {
            data = { Data: JSON.stringify({ Modify: { employee: [modify] } }) }
          } else {
            data = {
              Data: JSON.stringify(
                {
                  Modify: { employee: [modify] },
                  Add: { employee: [add] }
                }
              )
            }
          }
          _this.loading = true
          $.ajax({
            url: DomainName + '/GoodsFileEdit',
            type: 'POST',
            data: data,
            success: function (res) {
              console.log('提交结果', res)
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.history.go(-1)
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deal() {
      const modify = deepClone(this.form)
      return { add: null, modify };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
})